package com.hoppinzq.service;

import com.hoppinzq.service.annotation.ServiceRegister;
import com.hoppinzq.service.annotation.ServiceRegisterMethod;
import com.hoppinzq.service.circuitBreaker.aop.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;


@ServiceRegister(title = "测试1", description = "这是测试1的描述")
public class Test1ServiceImpl implements Test1Service{
    @Autowired
    private HttpServletRequest request;

    @Override
    @ServiceRegisterMethod(title = "方法测试",description = "打印test1")
    public String sayTest(String msg) {
        String traceId=request.getHeader("traceId");
        return "test1:"+msg+",traceId:"+traceId;
    }

    @Override
    @ServiceRegisterMethod(title = "方法测试2",description = "打印i+j")
    @CircuitBreaker(threshold = 2,timeout = 2000,aliveTime = 3000,fallback = "getAddFallback")
    public String getAdd(int i, int j){
        //若j<0,报错除数不能为0；若j为0，手动抛出异常；若j>0，随眠jms模拟超时操作
        if(j<0){
            i=j/0;
        }
        if(j==0){
            throw new RuntimeException("j不能是0");
        }
        try{
            Thread.sleep(j);
        }catch (Exception ex){}
        return "i+j="+(i+j);
    }
    //上面方法报错或者超时，直接执行熔断方法
    public String getAddFallback(){
        return "服务正在忙碌";
    }
}
